/**
 * @Project TMHSC
 * @Author Tran Manh Hung
 * @Email: hungtmit@gmail.com | tmh@xwebgate.com
 * @Copyright © 2011 TMH. All rights reserved
 * @Createdate Feb 1, 2011
 * @Createtime 9:46:24 PM
 */

package com.tmh.study.aASA
{
	import flash.display.Sprite;
	import flash.events.Event;

	public class FlockTest extends Sprite
	{
		private var _vehicles:Array;

		private var numVehicles:int = 10;

		public function FlockTest()
		{
			_vehicles = new Array();

			for (var i:int = 0; i < numVehicles; i++)
			{
				var vehicle:SteeredVehicle = new SteeredVehicle();
				vehicle.position = new Vector2D(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight);
				vehicle.velocity = new Vector2D(Math.random() * 4 - 2, Math.random() * 4 - 2);
				vehicle.maxSpeed = Math.random() * 4 + 4;
				vehicle.edgeBehavior = Vehicle.BOUNCE;

				_vehicles.push(vehicle);
				addChild(vehicle);
			}

			addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}

		private function enterFrameHandler(event:Event):void
		{
			for (var i:int; i < numVehicles; i++)
			{
				(_vehicles[i] as SteeredVehicle).flock(_vehicles);
				(_vehicles[i] as SteeredVehicle).update();
			}
		}
	}
}